//
//  FeedBackVC.m
//  HengYouCai
//
//  Created by ycgwl on 2017/5/4.
//  Copyright © 2017年 lwj. All rights reserved.
//

#import "FeedBackVC.h"
#import "UITextView+PlaceHolder.h"
@interface FeedBackVC ()<UIScrollViewDelegate,UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *ideaTV;
@property (weak, nonatomic) IBOutlet UIButton *certainBtn;
@property (weak, nonatomic) IBOutlet UILabel *wordCount;

@end

@implementation FeedBackVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.titleName; //@"意见反馈";
    _ideaTV.delegate = self;
    WLGetWeakSelf(weakSelf, self);

    self.ideaTV.placeholder = @"请输入您对我们平台的宝贵意见...";
    self.ideaTV.placeholderColor = [UIColor colorWithHexString:@"#bbbbbb"];
    
    [self.ideaTV.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length>0) {
            weakSelf.certainBtn.backgroundColor = Normal_RedColor;
            weakSelf.certainBtn.enabled = YES;

        }else{
            weakSelf.certainBtn.backgroundColor = [UIColor colorWithHexString:@"#d9d9d9"];
            weakSelf.certainBtn.enabled = NO;

        }
    }];
    [[_certainBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf.view endEditing:YES];
        
        if (![[WLFileManager readUserDataForKey:kUserSessionId] length]) {
            [weakSelf presentViewController:(UIViewController *)LogInVC animated:YES completion:nil];
            return;
        }
        
        [weakSelf.tableView showHUDLoadingTips:@"保存中..."];
        [HYCNetService feedBackWithDict:@{@"content":_ideaTV.text,@"deviceType":@"0"} onSuccess:^(NSDictionary *dictResult) {
            //
            double delayInSeconds = 1.0;
            [weakSelf.tableView showHUDSuccessTips:dictResult[@"message"] hideDelay:delayInSeconds];
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
            dispatch_after(popTime, dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];

            });
        } andFailed:^(NSInteger code, NSString *errorMsg) {
            //
            [weakSelf.view showHUDFailTips:errorMsg hideDelay:kHUDHideDelay];
        }];
    }];
    
}
/**
 用于计算textview输入情况下的字符数
 
 */
- (NSInteger)getInputLengthWithText:(NSString *)text
{
    NSInteger textLength = 0;
    //获取高亮部分
    UITextRange *selectedRange = [_ideaTV markedTextRange];
    if (selectedRange) {
        NSString *newText = [_ideaTV textInRange:selectedRange];
        textLength = (newText.length + 1) / 2 + [_ideaTV offsetFromPosition:_ideaTV.beginningOfDocument toPosition:selectedRange.start] + text.length;
    } else {
        textLength = _ideaTV.text.length + text.length;
    }
    return textLength;
}
#pragma textViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSInteger textLength = [self getInputLengthWithText:text];
    _wordCount.text = [NSString stringWithFormat:@"%ld",200-textLength];
    if (textLength >= 200) {
        //超过100个字可以删除
        if ([text isEqualToString:@""]) {
            return YES;
        }
        return NO;
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView
{
    if ([self getInputLengthWithText:nil] > 100) {
        textView.text = [textView.text substringToIndex:100];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (fabs(scrollView.contentOffset.y)>40) {
        [self.view endEditing:YES];
    }
}

@end
